package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.pay.dto.QueryStatusRequest;
import com.mall.seckill.SeckillService;
import com.mall.seckill.dto.*;
import com.mall.user.annotation.Anoymous;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lichao
 * @createdate 2021-01-29 16:07
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class SeckillController {

    @Reference(check = false,timeout = 500,retries = 0)
    SeckillService secKillService;

    /**
     * 秒杀列表
     * @param sessionId
     * @return
     */
    @Anoymous
    @RequestMapping("/seckilllist")
    public ResponseData seckilllist(Integer sessionId){
        SecKilllistRequest request = new SecKilllistRequest();
        request.setSessionId(sessionId);
        SecKilllistResponse response = secKillService.seckilllist(request);
        return new ResponseUtil<SecKilllistResponse>().setData(response);
    }

    @PostMapping("/promoProductDetail")
    public ResponseData promoProductDetail(@RequestBody PromoProductDetailRequest request){

        PromoProductDetailResponse response = secKillService.promoProductDetail(request);
        return new ResponseUtil<PromoProductDetailResponse>().setData(response);
    }

    /**
     * 秒杀下单
     * @param seckillRequest
     * @param request
     * @return
     */
    @PostMapping("/seckill")
    public ResponseData seckill(@RequestBody SeckillRequest seckillRequest, HttpServletRequest request){
        seckillRequest.requestCheck();
        String userInfo = (String) request.getAttribute("userInfo");
        SeckillResponse seckillResponse = secKillService.seckill(seckillRequest,userInfo);
        if (seckillResponse.equals("500")){
            log.error("秒杀失败");
            return new ResponseUtil<String>().setErrorMsg("秒杀失败");
        }
        log.info("秒杀成功");
        return new ResponseUtil<SeckillResponse>().setData(seckillResponse);
    }


}
